package server

import (
	"gitee.com/cdsailyj/go-vpn/app/index/router"
	"gitee.com/cdsailyj/go-vpn/common/tool"
	"gitee.com/cdsailyj/go-vpn/global"
	"gitee.com/cdsailyj/go-vpn/server/cmd"
	"gitee.com/cdsailyj/go-vpn/server/task"
	"github.com/gin-gonic/gin"
	"golang.org/x/sync/errgroup"
	"log"
)

func Run() {
	log.Printf("当前环境变量:global.CONFIG.AppEnv:%v \t gin.Mode:%v \n", global.AppEnv, gin.Mode())

	if len(global.Cmd) > 0 {
		cmdServer()
		return
	}
	apiServer()
	task.StartCron()
}

func cmdServer() {
	log.Printf("命令行参数:%#v\n", tool.GetCmdParams())
	cmd.StartCmd()
}

func apiServer() {
	//用于启动多个服务
	var g errgroup.Group

	//监听端口
	g.Go(func() error {
		r := router.SetupRouter()
		//tool.OpenBrowser("http://127.0.0.1:8892")
		return r.Run(":8892")
	})

	if err := g.Wait(); err != nil {
		log.Fatal(err)
	}
}
